import time

from fastapi import FastAPI
from paddleocr import PaddleOCR

# Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
# 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
ocr = PaddleOCR(use_angle_cls=True, lang="ch", use_gpu=False, show_log=False)
# need to run only once to download and load model into memory


# img_path = './imgs/001.jpg'
# img_path = './imgs/101.png'
# img_path = 'ftp://root:123456@192.168.8.188/gin-vue-admin/web/src/assets/github.png'
# img_path = 'https://res-static.hc-cdn.cn/cloudbu-site/china/zh-cn/wangxue/header/logo.svg' # 不支持
# img_path = 'https://7n.w3cschool.cn/statics/images/logonew2.png'  # http图片可以解析

app = FastAPI()


@app.get("/")
def read_root(img_path: str):
    # 获取开始时间
    start = time.perf_counter()
    result = ocr.ocr(img_path, cls=True)
    # 获取结束时间
    end = time.perf_counter()
    cost = round(end - start, 6)
    print("运行时间：", cost, "秒")
    data = []
    for idx in range(len(result)):
        res = result[idx]
        for line in res:
            data.append(line[1][0])

    return {"data": data, "cost": cost}

# uvicorn main:app --port=8088 --host=0.0.0.0 --workers=1